﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using MyCraft.Minecraft_2D.Blocs;
using Microsoft.Xna.Framework;

namespace MyCraft.Minecraft_2D
{
    public class BlocSet
    {
        #region Attributs
        public delegate Bloc bloc(Vector2 position, double timer);
        public Dictionary<TypeBloc, bloc> _newBlocs;
        private Dictionary<TypeBloc, Bloc> _blocs;
        public Dictionary<TypeBloc, Texture2D> _textures;
        private static BlocSet _singleton;
        private ContentManager _content;
        #endregion

        #region Constructeur
        private BlocSet()
        {
            //Bloc del3 = delegate()
            //{ return new Bloc(false) { Id = 0, Name = "Air", Percent = 0 }; };
            _newBlocs = new Dictionary<TypeBloc, bloc>
            {
                { TypeBloc.Air, delegate(Vector2 position, double timer) { return new Bloc(0, 0, false); }},
                { TypeBloc.Stone, delegate(Vector2 position, double timer) { return new Stone(timer); }},
                { TypeBloc.Grass, delegate(Vector2 position, double timer) { return new Grass(timer); }},
                { TypeBloc.Wood, delegate(Vector2 position, double timer) { return new Wood(position, timer, _content); }},
                { TypeBloc.Plank, delegate(Vector2 position, double timer) { return new Plank(position, timer, _content); }},
                { TypeBloc.BedRock, delegate(Vector2 position, double timer) { return new BedRock(timer); }},
                { TypeBloc.Dirt, delegate(Vector2 position, double timer) { return new Dirt(timer); }},
                { TypeBloc.Leaves, delegate(Vector2 position, double timer) { return new Leaves(timer); }},
                { TypeBloc.Cavern, delegate(Vector2 position, double timer) { return new Cavern(timer); }},
                { TypeBloc.Cobblestone, delegate(Vector2 position, double timer) { return new Cobblestone(timer); }},
                { TypeBloc.Glass, delegate(Vector2 position, double timer) { return new Glass(timer); }}
            };

            _textures = new Dictionary<TypeBloc, Texture2D>();

            _blocs = new Dictionary<TypeBloc, Bloc>();
            for (int i = 0; i < 11; i++)
                _blocs.Add((TypeBloc)i, _newBlocs[(TypeBloc)i].Invoke(new Vector2(0.5f), 0));
        }
        #endregion

        #region Propriétés
        public int Percent
        {
            get
            {
                int p = 0;
                foreach (var item in _blocs.Values)
                    p += item.Percent;
                return p;
            }
        }

        public int Count
        {
            get { return _newBlocs.Count; }
        }

        public Bloc this[TypeBloc type]
        {
            get { return (Bloc)_blocs[type]; }
        }
        #endregion

        #region Methodes
        public static BlocSet GetSingleton()
        {
            if (_singleton == null)
                _singleton = new BlocSet();
            return _singleton;
        }

        public void LoadContent(ContentManager content)
        {
            _content = content;
            for (int i = 0; i < 11; i++)
            {
                _textures.Add((TypeBloc)i, content.Load<Texture2D>(@"Blocs\" + _blocs[(TypeBloc)i].Name));
            }
        }
        #endregion
    }
}
